package com.softtown.modules.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.softtown.common.constant.Constant;
import com.softtown.common.enums.Status;
import com.softtown.common.page.PageData;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.organization.dao.CompOrgPosnDao;
import com.softtown.modules.organization.dto.CompOrgPosnDTO;
import com.softtown.modules.organization.entity.CompOrgPosnEntity;
import com.softtown.modules.organization.service.CompOrgPosnService;
import com.softtown.modules.organization.vo.CompOrgPosnVO;
import com.softtown.modules.sys.enums.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompOrgPosnServiceImpl
 * @Description: 公司岗位Service实现层
 * @Author: fuming
 * @Date: 2019/6/5 10:55
 */
@Service
public class CompOrgPosnServiceImpl extends BaseServiceImpl<CompOrgPosnDao, CompOrgPosnEntity> implements CompOrgPosnService {

    /**
     * @Title: getPosnListByCompId
     * @Description: 根据公司所属的ID来获取相应的所有岗位Service实现方法
     * @param @param [companyId]
     * @return @return java.util.List<com.softtown.modules.organization.vo.CompOrgPosnVO>
     * @author fuming
     * @throws
     * @date 2019/6/5 10:58
     */
    @Override
    public List<CompOrgPosnVO> getPosnListByCompId(Long companyId) {
        List<CompOrgPosnEntity> posnListByCompId = baseDao.getPosnListByCompId(companyId);
        return ConvertUtils.sourceToTarget(posnListByCompId, CompOrgPosnVO.class);
    }

    @Override
    public List<CompOrgPosnVO> getCompExecPosn(Long companyId) {
        List<CompOrgPosnEntity> posnListByCompId = baseDao.getCompExecPosn(companyId);
        return ConvertUtils.sourceToTarget(posnListByCompId, CompOrgPosnVO.class);
    }

    /**
     * @Title: addPosition
     * @Description: 岗位添加Service实现方法
     * @param @param [compOrgPosnDTO]
     * @return @return void
     * @author fuming
     * @throws
     * @date 2019/6/9 13:45
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPosition(CompOrgPosnDTO compOrgPosnDTO) {
        CompOrgPosnEntity compOrgPosnEntity = ConvertUtils.sourceToTarget(compOrgPosnDTO, CompOrgPosnEntity.class);
        compOrgPosnEntity.setCompId(compOrgPosnDTO.getCompId());
        compOrgPosnEntity.setName(compOrgPosnDTO.getName());
        compOrgPosnEntity.setSort(Integer.valueOf(compOrgPosnDTO.getSort()));
        compOrgPosnEntity.setRemark(compOrgPosnDTO.getRemark());
        compOrgPosnEntity.setStatus(Status.NORMAL.value());
        insert(compOrgPosnEntity);
    }

    /**
     * @Title: updatePosnInfos
     * @Description: 岗位修改Service实现方法
     * @param @param [compOrgPosnDTO]
     * @return @return boolean
     * @author fuming
     * @throws
     * @date 2019/6/24 22:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePosnInfos(CompOrgPosnDTO compOrgPosnDTO) {
        CompOrgPosnEntity compOrgPosnEntity = ConvertUtils.sourceToTarget(compOrgPosnDTO, CompOrgPosnEntity.class);
        compOrgPosnEntity.setId(compOrgPosnDTO.getId());
        compOrgPosnEntity.setName(compOrgPosnDTO.getName());
        compOrgPosnEntity.setSort(Integer.valueOf(compOrgPosnDTO.getSort()));
        compOrgPosnEntity.setRemark(compOrgPosnDTO.getRemark());
        compOrgPosnEntity.setUpdater(compOrgPosnDTO.getUpdater());
        compOrgPosnEntity.setUpdateDate(new Date());
        compOrgPosnEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        compOrgPosnEntity.setIsDeleted(0);
        baseDao.updatePosnInfos(compOrgPosnEntity);
    }

    /**
     * @Title: getPosnInfoById
     * @Description: 根据id去找出相对应的岗位信息Service实现方法
     * @param @param [id]
     * @return @return com.softtown.modules.organization.vo.CompOrgPosnVO
     * @author fuming
     * @throws
     * @date 2019/6/15 14:12
     */
    @Override
    public CompOrgPosnVO getPosnInfoById(Long id) {
        CompOrgPosnEntity posnInfoById = baseDao.getPosnInfoById(id);
        return ConvertUtils.sourceToTarget(posnInfoById, CompOrgPosnVO.class);
    }

    @Override
    public PageData<CompOrgPosnVO> page(Map<String, Object> params) {
        // isAsc为false,降序Desc
        IPage<CompOrgPosnEntity> page = baseDao.selectPage(getPage(params, Constant.SORT, true), getWrapper(params));
        return getPageData(page, CompOrgPosnVO.class);
    }

    @Override
    public CompOrgPosnVO getPositionInfoById(Long id) {
        CompOrgPosnEntity positionInfoById = baseDao.getPositionInfoById(id);
        return ConvertUtils.sourceToTarget(positionInfoById, CompOrgPosnVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePosition(Long id) {
        CompOrgPosnEntity positionInfoById = baseDao.getPositionInfoById(id);
        positionInfoById.setIsDeleted(null);
        baseDao.updatePosnInfos(positionInfoById);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void activeOrgPosn(Long id) {
        CompOrgPosnEntity positionInfoById = baseDao.getPositionInfoById(id);
        if (positionInfoById.getStatus().equals(StatusEnum.FORBIDDENSTATUS.value())) {
            positionInfoById.setStatus(StatusEnum.ACTIVATESTATUS.value());
        } else {
            positionInfoById.setStatus(StatusEnum.FORBIDDENSTATUS.value());
        }
        baseDao.updatePosnInfos(positionInfoById);
    }

    private Wrapper<CompOrgPosnEntity> getWrapper(Map<String, Object> params) {
        String compId = (String) params.get("compId");
        QueryWrapper<CompOrgPosnEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(compId), "comp_id", compId);
        wrapper.like("is_deleted", "0");
        return wrapper;
    }

    @Override
    public List<CompOrgPosnEntity> getFindPosnByCompId(Long compId) {
        return baseDao.getFindPosnByCompId(compId);
    }

    @Override
    public CompOrgPosnEntity getPosnById(Long id) {
        return baseDao.getPosnInfoById(id);
    }
}
